package web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import beans.Tag;

import dao.NewsDAO;
import dao.UserDAO;
import dao.TagDAO;

public class SzukajController extends MultiActionController{

	private UserDAO userDAO;
	private TagDAO tagDAO;
	
	public void setTagDAO(TagDAO tagDAO){
		this.tagDAO = tagDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	
	private NewsDAO newsDAO;

	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}

	public ModelAndView szukaj(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			ModelMap modelMap = new ModelMap();
			modelMap.addAttribute("usersList", userDAO.GetAllUsers());
			modelMap.addAttribute("tagList", tagDAO.GetAllTag());
			modelMap.addAttribute("user", userDAO.getLoggedUser());
			return new ModelAndView("szukaj", modelMap);
			
	}
	
	public ModelAndView searchName(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
		
		String queryString = request.getParameter("searchAdress");
		System.out.println(queryString);
		modelMap.addAttribute("resultList", newsDAO.searchName(queryString));
		modelMap.addAttribute("tagList", tagDAO.GetAllTag());
		modelMap.addAttribute("user", userDAO.getLoggedUser());
		return new ModelAndView("szukaj", modelMap);
	}
	
	public ModelAndView searchByTag (HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
			
		String queryString = ServletRequestUtils.getRequiredStringParameter(request, "tag");

		/*Tag tag = tagDAO.FindTagById(id);
		String queryString = tag.getName();	*/
				
		modelMap.addAttribute("resultTagList", newsDAO.searchByTag(queryString));
		modelMap.addAttribute("tagList", tagDAO.GetAllTag());
		modelMap.addAttribute("user", userDAO.getLoggedUser());
		return new ModelAndView("szukaj", modelMap);
	}
}
